package controller_common

import (
	"github.com/gin-gonic/gin"
	"strings"
	"tools-api/src/buisiness/service/service_common"
	"tools-api/src/routes/request"
	"tools-api/src/routes/response"
)

// VisitTrack 获取当前计数
func VisitTrack(ctx *gin.Context) {
	idKey := "X-Visitor-Id"
	ip := request.GetClientIP(ctx)                 // 获取客户端IP
	uid := strings.TrimSpace(ctx.GetHeader(idKey)) // 从请求头获取访客ID

	result, err := service_common.VisitTrack(ip, uid)
	if err != nil {
		response.FailByCode(ctx, 500, "服务器内部错误")
		return
	}
	// 返回响应
	ctx.Header("Access-Control-Expose-Headers", idKey)
	ctx.Header(idKey, result["uid"].(string))
	delete(result, "uid")
	response.Success(ctx, result)
}
